Skip to content

ChangeHeadersProxyModel

Base classes

Name Children Inherits
IdentityProxyModel
prettyqt.core.identityproxymodel
Proxies its source model unmodified.

â‹” Inheritance diagram

graph TD
  828252285["itemmodels.ChangeHeadersProxyModel"]
  527136029["core.IdentityProxyModel"]
  2431222121["core.AbstractProxyModelMixin"]
  3882849576["core.AbstractItemModelMixin"]
  3227531497["core.ObjectMixin"]
  2751680898["builtins.object"]
  3668892142["QtCore.QIdentityProxyModel"]
  3989152870["QtCore.QAbstractProxyModel"]
  1898060022["QtCore.QAbstractItemModel"]
  3984060476["QtCore.QObject"]
  37984305["Shiboken.Object"]
  527136029 --> 828252285
  2431222121 --> 527136029
  3882849576 --> 2431222121
  3227531497 --> 3882849576
  2751680898 --> 3227531497
  3668892142 --> 527136029
  3989152870 --> 3668892142
  1898060022 --> 3989152870
  3984060476 --> 1898060022
  37984305 --> 3984060476
  2751680898 --> 37984305

🛈 DocStrings

Bases: IdentityProxyModel

Proxy model for changing the header data (either horizontal or vertical).

Header data can either be changed by passing a list with same length as source length or by passing a dictionary with index as key and new value as value (Example: {1: "abc", 3: "def"} changes section 1 to "abc" and section 3 to "def") Apart from the regular use case of changing the text, the other roles can be changed, too.

Example

table.proxifier.change_headers(
    header=["x", "y", "z"],
    orientation=constants.HORIZONTAL,
    role=constants.DISPLAY_ROLE,
)
table.show()
# or
model = MyModel()
proxy = ChangeHeadersProxyModel(
    header=["x", "y", "z"], orientation=constants.VERTICAL
)
proxy.set_source_model(model)
table.set_model(proxy)
table.show()
Source code in prettyqt/itemmodels/proxies/changeheadersproxymodel.py
class ChangeHeadersProxyModel(core.IdentityProxyModel):
    """Proxy model for changing the header data (either horizontal or vertical).

    Header data can either be changed by passing a list with same length as source length
    or by passing a dictionary with index as key and new value as value
    (Example: {1: "abc", 3: "def"} changes section 1 to "abc" and section 3 to "def")
    Apart from the regular use case of changing the text, the other roles can be changed,
    too.

    ### Example

    ```py
    table.proxifier.change_headers(
        header=["x", "y", "z"],
        orientation=constants.HORIZONTAL,
        role=constants.DISPLAY_ROLE,
    )
    table.show()
    # or
    model = MyModel()
    proxy = ChangeHeadersProxyModel(
        header=["x", "y", "z"], orientation=constants.VERTICAL
    )
    proxy.set_source_model(model)
    table.set_model(proxy)
    table.show()
    ```
    """

    ID = "change_headers"
    ICON = "table-headers-eye"

    def __init__(
        self,
        header: list[Any] | dict[int, Any],
        orientation: constants.Orientation
        | constants.OrientationStr = constants.HORIZONTAL,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
        **kwargs,
    ):
        super().__init__(**kwargs)
        self._orientation = constants.ORIENTATION.get_enum_value(orientation)
        self._header = header
        self._role = role

    def setSourceModel(self, model):
        header_len = (
            model.columnCount()
            if self._orientation == constants.HORIZONTAL
            else model.rowCount()
        )
        if isinstance(self._header, list) and len(self._header) != header_len:
            msg = "list needs to be same list as header"
            raise ValueError(msg)
        super().setSourceModel(model)

    def get_header(self) -> list[int]:
        return self._header

    def set_header(
        self,
        header: list[str] | dict[int, str],
        orientation: constants.Orientation | constants.OrientationStr,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    ):
        with self.reset_model():
            self._header = header
            self._orientation = constants.ORIENTATION.get_enum_value(orientation)
            self._role = role

    def headerData(
        self,
        section: int,
        orientation: constants.Orientation,
        role: constants.ItemDataRole = constants.DISPLAY_ROLE,
    ):
        if orientation == self._orientation and role == self._role:  # noqa: SIM102
            if (isinstance(self._header, dict) and section in self._header) or isinstance(
                self._header, list
            ):
                return self._header[section]
        return self.sourceModel().headerData(section, orientation, role)

    header = core.Property(
        object,
        get_header,
        set_header,
        doc="New headers (dict / list)",
    )

:fa-brands-github: Show source on GitHub